package products

import (
	"cloud_android_backend/api/common"
	"cloud_android_backend/pkg/middleware/authorization"
	orders2 "cloud_android_backend/pkg/models/orders"
	model "cloud_android_backend/pkg/models/users"
	"cloud_android_backend/pkg/modules/orders"
	"cloud_android_backend/pkg/modules/products"
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/golang/glog"
	rb "modules.tenxcloud.com/common/middleware"
	"net/http"
	"strconv"
)

func Register(application *gin.RouterGroup) {
	application.GET("/products/list", GetProductsAndPackagesList)
	application.POST("/products/payment", authorization.TokenCheck, rb.RequestBody, Payment)
	application.GET("/products/packages", GetProductPackagesList)
}

func GetProductsAndPackagesList(c *gin.Context) {
	res, err := products.GetProductsLists()
	if err != nil {
		glog.Error(err)
		c.AbortWithStatusJSON(common.ErrorResponse(err.Error()))
		return
	}
	c.JSON(http.StatusOK, common.SuccessResponse(res))
}
func GetProductPackagesList(c *gin.Context) {
	produductId := c.Query("product_id")
	produductIdInt, _ := strconv.Atoi(produductId)
	res, err := products.GetProductPackagesList(produductIdInt)
	if err != nil {
		glog.Error(err)
		c.AbortWithStatusJSON(common.ErrorResponse(err.Error()))
		return
	}
	c.JSON(http.StatusOK, common.SuccessResponse(res))

}

type PhonePaymentReq struct {
	ProductId        int    `json:"product_id"`
	PackageId        int    `json:"package_id"`
	Quantity         int    `json:"quantity"`
	PaymentSource    string `json:"payment_source"`
	AutoRenew        bool   `json:"auto_renew"`
	AndroidVersion   string `json:"android_version"`
	RealPhoneModelId int64  `json:"real_phone_model_id"`
	ProxyId          int    `json:"proxy_id"`
}

func Payment(c *gin.Context) {
	li, exists := c.Get(authorization.LoginUserKey)
	if !exists {
		c.AbortWithStatusJSON(common.ErrorResponse("get user from content failed"))
		return
	}
	current := li.(*model.User)
	body, _ := c.Get(rb.RequestBodyKey)
	paymentForm := PhonePaymentReq{}
	if err := json.Unmarshal(body.([]byte), &paymentForm); err != nil {
		glog.Error(err)
		c.AbortWithStatusJSON(common.ErrorResponse(err.Error()))
		return
	}

	fmt.Println(current)
	orderForm := orders.CreateOrderReq{
		PaymentSource: paymentForm.PaymentSource,
		TransType:     int(orders2.TransType_Recharge),
	}
	orderForm.ClientIp = c.ClientIP()
	orderForm.DeviceInfo = c.Request.UserAgent()
	paymentParam := orders.CreateInstanceOrderReq{
		ProductId:        paymentForm.ProductId,
		PackageId:        paymentForm.PackageId,
		Quantity:         paymentForm.Quantity,
		AutoRenew:        paymentForm.AutoRenew,
		AndroidVersion:   paymentForm.AndroidVersion,
		CreateOrderReq:   orderForm,
		RealPhoneModelId: paymentForm.RealPhoneModelId,
		ProxyId:          paymentForm.ProxyId,
	}

	result, err := orders.CreateInstanceOrderAndPay(paymentParam, current.UserID)
	if err != nil {
		glog.Error(err)
		c.AbortWithStatusJSON(common.ErrorResponse(err.Error()))
		return
	}
	c.JSON(http.StatusOK, common.SuccessResponse(result))
}
